import dayjs from 'dayjs'
import axios from 'axios'
export const apiUrl = 'http://127.0.0.1:8001'

/**
 * 格式化日期时间
 * @param {*} val
 * @param {*} format = 'YYYY-MM-DD'
 * @returns
 */
export const formatTime = (val, format = 'YYYY-MM-DD') => {
  if (!isNull(val)) {
    val = parseInt(val) * 1000
    return dayjs(val).format(format)
  } else {
    return ''
  }
}

/**
 * 判断是否为空
 * @param {*} data
 * @returns
 */
export const isNull = (data) => {
  if (!data) return true
  if (JSON.stringify(data) === '{}') return true
  if (JSON.stringify(data) === '[]') return true
}

// downloadFile

export const downloadFile = (file_url) => {
  return axios({
    url: apiUrl + file_url,
    method: 'get',
    responseType: 'blob',
    data: {}
  })
}

/**
 * 全局挂载
 */
export default (app) => {
  app.config.globalProperties.$common = {
    formatTime,
    apiUrl
  }
}
